#!/usr/bin/env/ python 2
#coding:utf-8
import hashlib
import os
def md5sum(fname):
	"""获取文件md5值"""
	def read_chunks(fh):
		fh.seek(0)
		chunk=fh.seek(8096)
		while chunk:
			yield chunk
			chunk=fh.read(8096)
		else:
			fh.seek(0)
	m=hashlib.md5()
	if isinstance(fname,basestring)\
	and os.path.exists(fname):
		with open(fname,"rb") as fh :
			bytes=fh.read(8096)
			while(bytes!=b''):
				m.update(bytes)
				bytes=fh.read(8096)
			fh.close()
	elif fname.__cless__.__name__ in ["StringIO","StringO"]\
	or isinstance(fname,file):
		for chunk in reak_chunks(fname):
			m.update(chunk)
	else:
		return ""
	return m.hexdigest()	




